﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class Solution46
    {
        private IList<IList<int>> res = new List<IList<int>>();

        public IList<IList<int>> Permute(int[] nums)
        {
            IList<int> path = new List<int>();
            bool[] visited = new bool[nums.Length];
            DFS(nums, path, visited);
            return res;
        }

        private void DFS(int[] nums, IList<int> path, bool[] visited)
        {
            if (path.Count == nums.Length)
            {
                res.Add(new List<int>(path));
                return;
            }

            for (int i = 0; i < nums.Length; i++)
            {
                if (!visited[i])
                {
                    visited[i] = true;

                    path.Add(nums[i]);
                    DFS(nums, path, visited);
                    //回溯
                    path.RemoveAt(path.Count - 1);
                    visited[i] = false;
                }
            }
        }
    }
}